package cn.jdy.plugins.admin.export;


import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import cn.jdy.common.util.EncodeUtil;
import cn.jdy.plugins.entity.Chat;
import cn.jdy.plugins.entity.RedPacket;


/**
 * @desc   导出Excel
 * @author jga
 * @date   2016-7-14
 */
public class RedPacketExcelView extends  AbstractExcelView {
	@Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<RedPacket> list = (List<RedPacket>) model.get("dataSet");
        HSSFSheet sheet = workbook.createSheet();
        sheet.setDefaultColumnWidth(12);

        HSSFCell cell = getCell(sheet,0,0);
        /*setText(cell, "时间");
        cell = getCell(sheet,0,1);
        setText(cell, (String) model.get("searchDate"));*/
        String rpType =  (String) model.get("rpType");
        
        cell = getCell(sheet,0,0);
        setText(cell, "用户昵称");
        cell = getCell(sheet,0,1);
        setText(cell, "支付时间");
        cell = getCell(sheet,0,2);
        if(rpType.equals("4")){
        	   setText(cell, "	打赏祝词");
       }else if(rpType.equals("3")){
    	   setText(cell, "操作类型");
       }else{
    	   setText(cell, "	祝词");
       }
     
        cell = getCell(sheet,0,3);
        setText(cell, "金额（元）");
        cell = getCell(sheet,0,4);
 
        
        for (int i = 0; i < list.size(); i++) {
        	HSSFRow row = sheet.createRow(i+1);
        	RedPacket obj = list.get(i);
            // 处理列
         //.createCell(0).setCellValue(obj.get("fromDate")+"");
          //  row.createCell(1).setCellValue(obj.get("toDate")+"");
          //  BigDecimal bd=(BigDecimal) obj.get("zdata"); 
          //  bd=bd.setScale(0, BigDecimal.ROUND_HALF_UP); 
          //转化为字符串输出 
        //  String OutString=bd.toString();
            row.createCell(0).setCellValue(obj.getUserNickName());
            row.createCell(1).setCellValue(obj.getCreateTime());
            row.createCell(2).setCellValue(obj.getWishMsg());
            row.createCell(3).setCellValue(obj.getTotal());
           
        }
        

        String fileName="记录";
        if(rpType.equals("4")){
             fileName = "打赏记录";
        }else if(rpType.equals("3")){
        	  fileName = "支付记录";
        }else{
        	 fileName = "红包记录";
        }
 
        // 处理中文文件名
        
        response.setHeader("Cache-Control", "private");  
        response.setHeader("Pragma", "private");  
        response.setContentType("application/vnd.ms-excel;charset=utf-8");  
        response.setHeader("Content-Type", "application/force-download");  
          
        fileName = EncodeUtil.processFileName(request, fileName);  
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls"); 
        
//        response.setContentType("application/vnd.ms-excel");
//        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();

    }
	
	
}
